package com.boc.startup;

import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import butterknife.BindView;
import demo.bocweb.com.sdk.base.activity.BaseActivity;
import demo.bocweb.com.sdk.config.ARoutConfig;
import demo.bocweb.com.sdk.config.AppConfig;
import demo.bocweb.com.sdk.util.SP;
import demo.bocweb.com.sdk.util.StatusBarUtils;

import static demo.bocweb.com.sdk.config.AppConfig.isNeedGuide;

/**
 * 启动页
 * Created by zhelun on 2018/2/1.
 */
@Route(path = ARoutConfig.splashPath)
public class SplashActivity extends BaseActivity {
    @BindView(R2.id.iv_image)
    ImageView ivImage;
    @BindView(R2.id.btn_click)
    Button btnClick;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initData() {
        super.initData();
        StatusBarUtils.with(this).init();
        if (!isTaskRoot()) {
            finish();
            return;
        }
        btnClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click();
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        new CountDownTimer(3000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                btnClick.setText("跳过" + millisUntilFinished / 1000 + "s");
            }

            @Override
            public void onFinish() {
                click();
            }
        }.start();
    }

    private void click() {
        if ((boolean) SP.get(AppConfig.count, false)) {
            if (AppConfig.isMustLogin) {
                //必须登录才能使用
                ARouter.getInstance().build(ARoutConfig.loginPath).greenChannel().navigation();
            } else {
                //无需登录可以进入主页面
                ARouter.getInstance().build(ARoutConfig.homeActivityPath).greenChannel().navigation();
            }
        } else {
            if (isNeedGuide) {
                //第一次打开应用，打开引导页
                ARouter.getInstance().build(ARoutConfig.guidePath).greenChannel().navigation();
            } else {
                //第一次打开应用，但该应用无引导页
                if (AppConfig.isMustLogin) {
                    //必须登录才能使用
                    ARouter.getInstance().build(ARoutConfig.loginPath).greenChannel().navigation();
                } else {
                    //无需登录可以进入主页面
                    ARouter.getInstance().build(ARoutConfig.homeActivityPath).greenChannel().navigation();
                }
            }

        }
        finish();
        SP.put(AppConfig.count, true);
    }

}
